const Monitor = require('../../utils/monitor.js').Monitor
const util = require('../../utils/util.js')
Page(Monitor.hookApp({
  data: {
    maxmaxBalence: 0, //可提现最大金额
    cash_money: '',
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls
  },
  submit() {
    let that = this
    wx.showModal({
      title: '提示',
      content: `是否确认提现${that.data.cash_money || 0}元`,
      success() {
        util.API('spwx/v1.Distribut/applyCash', {
          distribute_type: that.data.distribute_type,
          distribute_lv_id: that.data.distribute_lv_id,
          cash_money: that.data.cash_money,
          store_id: that.data.store_id
        }).then(res => {
          let {
            msg,
            code
          } = res
          if (code == 200) {
            wx.navigateBack({
              delta: 1,
              success() {
                wx.showToast({
                  title: msg + '',
                  icon: 'none'
                })
              }
            })
          } else {
            wx.showToast({
              title: msg + '',
              icon: 'none'
            })
          }
        })
      }
    })

  },
  cashAll() { // 全部提现
    this.setData({
      cash_money: this.data.cash_max_money
    })
  },
  getVal(e) {
    this.setData({
      cash_money: e.detail.value
    })
  },
  getRules() {
    util.API('spwx/v1.Distribut/showDistributeSetting').then(res => {
      let {
        code,
        data,
        msg
      } = res
      if (code == 200) {
        let ret = data.return,
          cash_max_money;
        if (Number(ret.cash_limit_max) > Number(data.member.cashmoney ? data.member.cashmoney : 0)) {
          cash_max_money = Number(data.member.cashmoney ? data.member.cashmoney : 0)
        } else {
          cash_max_money = Number(ret.cash_limit_max)
        }

        console.log(`cash_max_money:${cash_max_money}   cash_limit_max:${Number(ret.cash_limit_max)}   cash_money : ${Number(data.member.cash_money ? data.member.cash_money : 0)}`)
        this.setData({
          distribute_lv_id: data.member.distribute_lv_id,
          distribute_type: data.member.distribute_type,
          store_id: data.member.store_id,
          cash_max_money,
          hand_settlement: data.return.hand_settlement,
          member: data.member,
          return: data.return
        })
      }
    })
  },
  onLoad: function (options) {

  },
  onShow() {
    this.getRules()
  }
}))